package daoyu.apps.controller;

import daoyu.apps.pojo.login.LoginBuilder;
import daoyu.apps.pojo.login.LoginReqDTO;
import daoyu.apps.pojo.login.LoginResDTO;
import daoyu.apps.service.ApiService;
import daoyu.common.result.AjaxResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * 抖音小程序与抖音交互接口
 */
@RestController
@Tag(name = "抖音api", description = "抖音小程序与抖音交互的api")
public class ApiController {


    @Autowired
    private ApiService appsApiService;

    @Autowired
    private LoginBuilder appsLoginBuilder;



    @GetMapping(value = "/login/{code}")
    @Operation(summary = "用户登录", description = "通过code进行用户登录")
    public AjaxResult login(@PathVariable("code") String code) {
        if (code == null || code.isEmpty() || code.equals("1")) {
            return AjaxResult.param_error("code 不能为空");
        }
        // 构建登录参数
        LoginReqDTO appsLoginReqDTO = appsLoginBuilder.loginReqDTOBuild(code);
        // 调用登录接口
        try {
            LoginResDTO appsLoginResponse = appsApiService.loginByCode(appsLoginReqDTO);

            return appsLoginBuilder.loginResVOBuild(appsLoginResponse);
        } catch (RuntimeException e){
            return AjaxResult.error(e.getMessage());
        }
    }
}
